package com.groupbuying.easyexcel;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MqConsumer {

    @Autowired
    private MqListener mqListener;

    public static final Logger LOGGER = LoggerFactory.getLogger(MqConsumer.class);


    public MqConsumer() {
    }

    @Bean
    public Consumer getOrderMQConsumer() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.GROUP_ID, Constant.MQ_GROUP_ID);
        properties.put(PropertyKeyConst.AccessKey, Constant.ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, Constant.SECRECT_KEY);
        properties.put(PropertyKeyConst.NAMESRV_ADDR, Constant.MQ_NAMESRV_ADDR);
        Consumer consumer = ONSFactory.createConsumer(properties);

        try {
            consumer.subscribe(Constant.MQ_TOPIC, Constant.MQ_ACCESS_TAG, mqListener);
            consumer.start();
            LOGGER.info("finance consumer is start !!!  groupId:{" + "GID_finance_test_ywxd_consumer" + "}");
        } catch (Exception exception) {
            LOGGER.error(String.format("finance consumer is error  groupId:{" + "GID_finance_test_ywxd_consumer" + "}", exception.getMessage(), exception));
        }

        return consumer;
    }
}
